package com.sfzd5.mistakesbook.db;

import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.TableUtils;

public class DBHelper extends OrmLiteSqliteOpenHelper {
    private static DBHelper mDatabaseHelper = null;

    private Dao<Grade, Integer> mGradeDao = null;
    private Dao<Exam, Integer> mExamDao = null;
    private Dao<Knowledge, Integer> mKnowledgeDao = null;
    private Dao<Mistakes, Integer> mMistakesDao = null;
    private Dao<Student, Integer> mStudentDao = null;

    private Dao<MistakesStudent, Integer> mMistakesStudentsDao = null;
    private Dao<MistakesKnowledge, Integer> mMistakesKnowledgeDao = null;

    private final static String DataBase_NAME = "mistakesbook.db";
    private final static int DataBase_VERSION = 2;

    private DBHelper(Context context, String databaseName,
                                 CursorFactory factory, int databaseVersion) {
        super(context, DataBase_NAME, factory, DataBase_VERSION);
    }

    public static DBHelper getInstance(Context context) {
        if (mDatabaseHelper == null) {
            mDatabaseHelper = new DBHelper(context, DataBase_NAME,
                    null, DataBase_VERSION);
        }
        return mDatabaseHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {

        Log.d(this.getClass().getName(), "ORMLite数据库 -> onCreate");

        try {
            TableUtils.createTableIfNotExists(connectionSource, Grade.class);
            TableUtils.createTableIfNotExists(connectionSource, Student.class);
            TableUtils.createTableIfNotExists(connectionSource, Exam.class);
            TableUtils.createTableIfNotExists(connectionSource, Knowledge.class);
            TableUtils.createTableIfNotExists(connectionSource, Mistakes.class);
            TableUtils.createTableIfNotExists(connectionSource, MistakesStudent.class);
            TableUtils.createTableIfNotExists(connectionSource, MistakesKnowledge.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database,
                          ConnectionSource connectionSource, int oldVersion, int newVersion) {

        Log.i(this.getClass().getName(), "数据库 -> onUpgrade");

        try {
            // 删除旧的数据库表。
            TableUtils.dropTable(connectionSource, Mistakes.class, true);
            TableUtils.dropTable(connectionSource, Knowledge.class, true);
            TableUtils.dropTable(connectionSource, Exam.class, true);
            TableUtils.dropTable(connectionSource, Student.class, true);
            TableUtils.dropTable(connectionSource, Grade.class, true);
            TableUtils.dropTable(connectionSource, MistakesStudent.class, true);
            TableUtils.dropTable(connectionSource, MistakesKnowledge.class, true);

            // 重新创建新版的数据库。
            onCreate(database, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Dao<MistakesStudent, Integer> getMistakesStudentDao() {
        if (mMistakesStudentsDao == null) {
            try {
                mMistakesStudentsDao = getDao(MistakesStudent.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mMistakesStudentsDao;
    }

    public Dao<MistakesKnowledge, Integer> getMistakesKnowledgeDao() {
        if (mMistakesKnowledgeDao == null) {
            try {
                mMistakesKnowledgeDao = getDao(MistakesKnowledge.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mMistakesKnowledgeDao;
    }

    public Dao<Exam, Integer> getExamDao() {
        if (mExamDao == null) {
            try {
                mExamDao = getDao(Exam.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mExamDao;
    }

    public Dao<Student, Integer> getStudentDao() {
        if (mStudentDao == null) {
            try {
                mStudentDao = getDao(Student.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mStudentDao;
    }

    public Dao<Grade, Integer> getGradeDao() {
        if (mGradeDao == null) {
            try {
                mGradeDao = getDao(Grade.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mGradeDao;
    }

    public Dao<Mistakes, Integer> getMistakesDao() {
        if (mMistakesDao == null) {
            try {
                mMistakesDao = getDao(Mistakes.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mMistakesDao;
    }

    public Dao<Knowledge, Integer> getKnowledgeDao() {
        if (mKnowledgeDao == null) {
            try {
                mKnowledgeDao = getDao(Knowledge.class);
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return mKnowledgeDao;
    }

    @Override
    public void close() {
        super.close();
        mExamDao = null;
        mStudentDao = null;
        mKnowledgeDao = null;
        mMistakesDao = null;
        mGradeDao = null;
    }

}
